﻿using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudApiServer.Services
{
    /// <summary>
    /// 进销存上传服务
    /// </summary>
    public class GoodsService
    {
        /// <summary>
        /// 3501 盘存上传
        /// </summary>
        /// <param name="fsiRequest">入参</param>
        /// <param name="ControllerName">来源控制器名称</param>
        /// <returns>出参</returns>
        public async Task<bool> GoodsUpload(FsiRequestHeader<InputGoodsUpload> fsiRequest, string ControllerName)
        {
            try
            {
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                string connectionString =
                    $"Database={ApiDatainfo.mysqlDatabase};Data Source={ApiDatainfo.mysqlUrl};Port={ApiDatainfo.mysqlPost};UserId={ApiDatainfo.mysqlUser};Password={ApiDatainfo.mysqlPwd};Charset=utf8mb4;TreatTinyAsBoolean=false;Allow User Variables=True";
                string InputJson = JsonConvert.SerializeObject(fsiRequest);
                string query = "INSERT INTO fsi_goodsupload  VALUES ( null,@fixmedins_code,@fixmedins_name, @med_list_codg,@fixmedins_hilist_id, @fixmedins_hilist_name, " +
                               "@rx_flag, @invdate, @inv_cnt, @manu_lotnum, @fixmedins_bchno, @manu_date, @expy_end, @memo, 0, @InputJson, now(),now())";
                var parameter = new List<MySqlParameter>
                {
                    new MySqlParameter("@fixmedins_code", fsiRequest.fixmedins_code),
                    new MySqlParameter("@fixmedins_name", fsiRequest.fixmedins_name),
                    new MySqlParameter("@med_list_codg", fsiRequest.Input.invinfo.med_list_codg),
                    new MySqlParameter("@fixmedins_hilist_id", fsiRequest.Input.invinfo.fixmedins_hilist_id),
                    new MySqlParameter("@fixmedins_hilist_name", fsiRequest.Input.invinfo.fixmedins_hilist_name),
                    new MySqlParameter("@rx_flag", fsiRequest.Input.invinfo.rx_flag),
                    new MySqlParameter("@invdate", fsiRequest.Input.invinfo.invdate),
                    new MySqlParameter("@inv_cnt", fsiRequest.Input.invinfo.inv_cnt),
                    new MySqlParameter("@manu_lotnum", fsiRequest.Input.invinfo.manu_lotnum),
                    new MySqlParameter("@fixmedins_bchno", fsiRequest.Input.invinfo.fixmedins_bchno),
                    new MySqlParameter("@manu_date", fsiRequest.Input.invinfo.manu_date),
                    new MySqlParameter("@expy_end", fsiRequest.Input.invinfo.expy_end),
                    new MySqlParameter("@memo", fsiRequest.Input.invinfo.memo),
                    new MySqlParameter("@InputJson", InputJson)
                };
                await MySqlHelper.ExecuteNonQueryAsync(connectionString, query, parameter);
                return true;
                
            }
            catch (Exception ex)
            {
                LogHelper.Error(
                    $"[GoodsService.GoodsUpload]盘存上传异常，错误内容：{ex.Message} 异常堆栈{ex.StackTrace}");
                throw;
            }
        }

        /// <summary>
        /// 3502 库存变更
        /// </summary>
        /// <param name="fsiRequest">入参</param>
        /// <param name="ControllerName">来源控制器名称</param>
        /// <returns>出参</returns>
        public async Task<bool> GoodsUpdate(FsiRequestHeader<InputGoodsUpdate> fsiRequest, string ControllerName)
        {
            try
            {
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                string connectionString =
                    $"Database={ApiDatainfo.mysqlDatabase};Data Source={ApiDatainfo.mysqlUrl};Port={ApiDatainfo.mysqlPost};UserId={ApiDatainfo.mysqlUser};Password={ApiDatainfo.mysqlPwd};Charset=utf8mb4;TreatTinyAsBoolean=false;Allow User Variables=True";
                string InputJson = JsonConvert.SerializeObject(fsiRequest);
                string query = "INSERT INTO fsi_goodsupdate  VALUES ( null,@fixmedins_code,@fixmedins_name, @med_list_codg,@inv_chg_type,@fixmedins_hilist_id, @fixmedins_hilist_name, " +
                               "@fixmedins_bchno, @pric, @cnt, @rx_flag, @inv_chg_time, @inv_chg_opter_name, @memo, @trdn_flag, 0, @InputJson, NOW(),NOW())";

                var parameter = new List<MySqlParameter>
                {
                    new MySqlParameter("@fixmedins_code", fsiRequest.fixmedins_code),
                    new MySqlParameter("@fixmedins_name", fsiRequest.fixmedins_name),
                    new MySqlParameter("@med_list_codg", fsiRequest.Input.invinfo.med_list_codg),
                    new MySqlParameter("@fixmedins_hilist_id", fsiRequest.Input.invinfo.fixmedins_hilist_id),
                    new MySqlParameter("@fixmedins_hilist_name", fsiRequest.Input.invinfo.fixmedins_hilist_name),
                    new MySqlParameter("@fixmedins_bchno", fsiRequest.Input.invinfo.fixmedins_bchno),
                    new MySqlParameter("@inv_chg_type", fsiRequest.Input.invinfo.inv_chg_type),
                    new MySqlParameter("@rx_flag", fsiRequest.Input.invinfo.rx_flag),
                    new MySqlParameter("@pric", fsiRequest.Input.invinfo.pric),
                    new MySqlParameter("@cnt", fsiRequest.Input.invinfo.cnt),
                    new MySqlParameter("@inv_chg_time", fsiRequest.Input.invinfo.inv_chg_time),
                    new MySqlParameter("@inv_chg_opter_name", fsiRequest.Input.invinfo.inv_chg_opter_name),
                    new MySqlParameter("@trdn_flag", fsiRequest.Input.invinfo.trdn_flag),
                    new MySqlParameter("@memo", fsiRequest.Input.invinfo.memo),
                    new MySqlParameter("@InputJson", InputJson)
                };
                await MySqlHelper.ExecuteNonQueryAsync(connectionString, query, parameter);
                return true;

            }
            catch (Exception ex)
            {
                LogHelper.Error(
                    $"[GoodsService.GoodsUpdate]库存变更异常，错误内容：{ex.Message} 异常堆栈{ex.StackTrace}");
                throw;
            }
        }

        /// <summary>
        /// 3503 采购上传
        /// </summary>
        /// <param name="fsiRequest">入参</param>
        /// <param name="ControllerName">来源控制器名称</param>
        /// <returns>出参</returns>
        public async Task<bool> GoodsBuy(FsiRequestHeader<InputGoodsBuy> fsiRequest, string ControllerName)
        {
            try
            {
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                string connectionString =
                    $"Database={ApiDatainfo.mysqlDatabase};Data Source={ApiDatainfo.mysqlUrl};Port={ApiDatainfo.mysqlPost};UserId={ApiDatainfo.mysqlUser};Password={ApiDatainfo.mysqlPwd};Charset=utf8mb4;TreatTinyAsBoolean=false;Allow User Variables=True";
                string InputJson = JsonConvert.SerializeObject(fsiRequest);
                string query =  "INSERT INTO fsi_goodsbuy  VALUES ( null,@fixmedins_code,@fixmedins_name,@med_list_codg,@fixmedins_hilist_id," +
                                " @fixmedins_hilist_name,@dynt_no,@fixmedins_bchno,@spler_name,@spler_pmtno," +
                                " @manu_lotnum,@prodentp_name, @aprvno, @manu_date,@expy_end," +
                                "@finl_trns_pric,@purc_retn_cnt,@purc_invo_codg,@purc_invo_no, @rx_flag," +
                                " @purc_retn_stoin_time, @purc_retn_opter_name, @prod_geay_flag,@memo,0,0," +
                                " @InputJson, now(),now())";
                var parameter = new List<MySqlParameter>
                {
                    new MySqlParameter("@fixmedins_code", fsiRequest.fixmedins_code),
                    new MySqlParameter("@fixmedins_name", fsiRequest.fixmedins_name),
                    new MySqlParameter("@med_list_codg", fsiRequest.Input.purcinfo.med_list_codg),
                    new MySqlParameter("@fixmedins_hilist_id", fsiRequest.Input.purcinfo.fixmedins_hilist_id),
                    new MySqlParameter("@fixmedins_hilist_name", fsiRequest.Input.purcinfo.fixmedins_hilist_name),

                    new MySqlParameter("@dynt_no", fsiRequest.Input.purcinfo.dynt_no),
                    new MySqlParameter("@fixmedins_bchno", fsiRequest.Input.purcinfo.fixmedins_bchno),
                    new MySqlParameter("@spler_name", fsiRequest.Input.purcinfo.spler_name),
                    new MySqlParameter("@spler_pmtno", fsiRequest.Input.purcinfo.spler_pmtno),
                    new MySqlParameter("@manu_lotnum", fsiRequest.Input.purcinfo.manu_lotnum),

                    new MySqlParameter("@prodentp_name", fsiRequest.Input.purcinfo.prodentp_name),
                    new MySqlParameter("@aprvno", fsiRequest.Input.purcinfo.aprvno),
                    new MySqlParameter("@manu_date", fsiRequest.Input.purcinfo.manu_date),
                    new MySqlParameter("@expy_end", fsiRequest.Input.purcinfo.expy_end),
                    new MySqlParameter("@finl_trns_pric", fsiRequest.Input.purcinfo.finl_trns_pric),

                    new MySqlParameter("@purc_retn_cnt", fsiRequest.Input.purcinfo.purc_retn_cnt),
                    new MySqlParameter("@purc_invo_codg", fsiRequest.Input.purcinfo.purc_invo_codg),
                    new MySqlParameter("@purc_invo_no", fsiRequest.Input.purcinfo.purc_invo_no),
                    new MySqlParameter("@rx_flag", fsiRequest.Input.purcinfo.rx_flag),
                    new MySqlParameter("@purc_retn_stoin_time", fsiRequest.Input.purcinfo.purc_retn_stoin_time),

                    new MySqlParameter("@purc_retn_opter_name", fsiRequest.Input.purcinfo.purc_retn_opter_name),
                    new MySqlParameter("@prod_geay_flag", fsiRequest.Input.purcinfo.prod_geay_flag),
                    new MySqlParameter("@memo", fsiRequest.Input.purcinfo.memo),
                    new MySqlParameter("@InputJson", InputJson)
                };
                await MySqlHelper.ExecuteNonQueryAsync(connectionString, query, parameter);
                return true;

            }
            catch (Exception ex)
            {
                LogHelper.Error(
                    $"[GoodsService.GoodsBuy]采购上传异常，错误内容：{ex.Message} 异常堆栈{ex.StackTrace}");
                throw;
            }
            
        }

        /// <summary>
        /// 3504 采购退货上传
        /// </summary>
        /// <param name="fsiRequest">入参</param>
        /// <param name="ControllerName">来源控制器名称</param>
        /// <returns>出参</returns>
        public async Task<bool> GoodsBuyReturn(FsiRequestHeader<InputGoodsBuy> fsiRequest, string ControllerName)
        {
            try
            {
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                string connectionString = $"Database={ApiDatainfo.mysqlDatabase};Data Source={ApiDatainfo.mysqlUrl};Port={ApiDatainfo.mysqlPost};UserId={ApiDatainfo.mysqlUser};Password={ApiDatainfo.mysqlPwd};Charset=utf8mb4;TreatTinyAsBoolean=false;Allow User Variables=True";
                string InputJson = JsonConvert.SerializeObject(fsiRequest); 
                var parameter = new List<MySqlParameter>
                {
                    new MySqlParameter("@fixmedins_code", fsiRequest.fixmedins_code),
                    new MySqlParameter("@fixmedins_name", fsiRequest.fixmedins_name),
                    new MySqlParameter("@med_list_codg", fsiRequest.Input.purcinfo.med_list_codg),
                    new MySqlParameter("@fixmedins_hilist_id", fsiRequest.Input.purcinfo.fixmedins_hilist_id),
                    new MySqlParameter("@fixmedins_hilist_name", fsiRequest.Input.purcinfo.fixmedins_hilist_name),

                    new MySqlParameter("@dynt_no", fsiRequest.Input.purcinfo.dynt_no),
                    new MySqlParameter("@fixmedins_bchno", fsiRequest.Input.purcinfo.fixmedins_bchno),
                    new MySqlParameter("@spler_name", fsiRequest.Input.purcinfo.spler_name),
                    new MySqlParameter("@spler_pmtno", fsiRequest.Input.purcinfo.spler_pmtno),
                    new MySqlParameter("@manu_lotnum", fsiRequest.Input.purcinfo.manu_lotnum),

                    new MySqlParameter("@prodentp_name", fsiRequest.Input.purcinfo.prodentp_name),
                    new MySqlParameter("@aprvno", fsiRequest.Input.purcinfo.aprvno),
                    new MySqlParameter("@manu_date", fsiRequest.Input.purcinfo.manu_date),
                    new MySqlParameter("@expy_end", fsiRequest.Input.purcinfo.expy_end),
                    new MySqlParameter("@finl_trns_pric", fsiRequest.Input.purcinfo.finl_trns_pric),

                    new MySqlParameter("@purc_retn_cnt", fsiRequest.Input.purcinfo.purc_retn_cnt),
                    new MySqlParameter("@purc_invo_codg", fsiRequest.Input.purcinfo.purc_invo_codg),
                    new MySqlParameter("@purc_invo_no", fsiRequest.Input.purcinfo.purc_invo_no),
                    new MySqlParameter("@rx_flag", fsiRequest.Input.purcinfo.rx_flag),
                    new MySqlParameter("@purc_retn_stoin_time", fsiRequest.Input.purcinfo.purc_retn_stoin_time),

                    new MySqlParameter("@purc_retn_opter_name", fsiRequest.Input.purcinfo.purc_retn_opter_name),
                    new MySqlParameter("@prod_geay_flag", fsiRequest.Input.purcinfo.prod_geay_flag),
                    new MySqlParameter("@memo", fsiRequest.Input.purcinfo.memo),
                    new MySqlParameter("@InputJson", InputJson)
                };
                
                string query = "select * from fsi_goodsbuy  where fixmedins_code =@fixmedins_code and med_list_codg =@med_list_codg and fixmedins_hilist_id =@fixmedins_hilist_id and fixmedins_bchno=@fixmedins_bchno and state =0 and datatype=0";
                DataTable dt = await MySqlHelper.ExecuteQueryAsync(connectionString, query, parameter);
                if (dt.Rows.Count < 1)
                {
                    throw new Exception($"查询采购上传记录表失败！根据传入的机构代码【{fsiRequest.fixmedins_code}】医保目录编码【{fsiRequest.Input.purcinfo.med_list_codg}】医疗机构内部编码【{fsiRequest.Input.purcinfo.fixmedins_hilist_id}】批次号【{fsiRequest.Input.purcinfo.fixmedins_bchno}】查询到的采购上传记录总数为：{dt.Rows.Count}");
                }
                query =
                    "INSERT INTO fsi_goodsbuy  VALUES ( null,@fixmedins_code,@fixmedins_name,@med_list_codg,@fixmedins_hilist_id," +
                    " @fixmedins_hilist_name,@dynt_no,@fixmedins_bchno,@spler_name,@spler_pmtno," +
                    " @manu_lotnum,@prodentp_name, @aprvno, @manu_date,@expy_end," +
                    "@finl_trns_pric,@purc_retn_cnt,@purc_invo_codg,@purc_invo_no, @rx_flag," +
                    " @purc_retn_stoin_time, @purc_retn_opter_name, @prod_geay_flag,@memo, 1,0," +
                    " @InputJson, now(),now())";
                await MySqlHelper.ExecuteNonQueryAsync(connectionString, query, parameter);
                return true;

            }
            catch (Exception ex)
            {
                LogHelper.Error(
                    $"[GoodsService.GoodsBuyReturn]采购退货上传异常，错误内容：{ex.Message} 异常堆栈{ex.StackTrace}");
                throw;
            }
        }

        /// <summary>
        /// 3505 销售上传
        /// </summary>
        /// <param name="fsiRequest">入参</param>
        /// <param name="ControllerName">来源控制器名称</param>
        /// <returns>出参</returns>
        public async Task<bool> GoodsSell(FsiRequestHeader<InputGoodsSell> fsiRequest, string ControllerName)
        {
            try
            {
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                string connectionString =
                    $"Database={ApiDatainfo.mysqlDatabase};Data Source={ApiDatainfo.mysqlUrl};Port={ApiDatainfo.mysqlPost};UserId={ApiDatainfo.mysqlUser};Password={ApiDatainfo.mysqlPwd};Charset=utf8mb4;TreatTinyAsBoolean=false;Allow User Variables=True";
                string InputJson = JsonConvert.SerializeObject(fsiRequest);
                string query = "INSERT INTO fsi_goodsSell VALUES ( " +
                               " null,@fixmedins_code,@fixmedins_name,@med_list_codg,@fixmedins_hilist_id," +
                               " @fixmedins_hilist_name,@fixmedins_bchno,@prsc_dr_cert_type,@prsc_dr_certno,@prsc_dr_name," +
                               " @phar_cert_type,@phar_certno, @phar_name, @phar_prac_cert_no,@hi_feesetl_type," +
                               " @setl_id,@mdtrt_sn,@psn_no,@psn_cert_type, @certno," +
                               " @psn_name, @manu_lotnum, @manu_date,@expy_end,@rx_flag," +
                               " @trdn_flag, @finl_trns_pric, @rxno,@rx_circ_flag,@rtal_docno," +
                               " @stoout_no, @bchno, @drug_trac_codg,@drug_prod_barc,@shelf_posi," +
                               " @sel_retn_cnt, @sel_retn_time, @sel_retn_opter_name,@memo,@mdtrt_setl_type," +
                               " 0,0,@InputJson, now(),now())";
                var parameter = new List<MySqlParameter>
                {
                    new MySqlParameter("@fixmedins_code", fsiRequest.fixmedins_code),
                    new MySqlParameter("@fixmedins_name", fsiRequest.fixmedins_name),
                    new MySqlParameter("@med_list_codg", fsiRequest.Input.selinfo.med_list_codg),
                    new MySqlParameter("@fixmedins_hilist_id", fsiRequest.Input.selinfo.fixmedins_hilist_id),

                    new MySqlParameter("@fixmedins_hilist_name", fsiRequest.Input.selinfo.fixmedins_hilist_name),
                    new MySqlParameter("@fixmedins_bchno", fsiRequest.Input.selinfo.fixmedins_bchno),
                    new MySqlParameter("@prsc_dr_cert_type", fsiRequest.Input.selinfo.prsc_dr_cert_type),
                    new MySqlParameter("@prsc_dr_certno", fsiRequest.Input.selinfo.prsc_dr_certno),
                    new MySqlParameter("@prsc_dr_name", fsiRequest.Input.selinfo.prsc_dr_name),

                    new MySqlParameter("@phar_cert_type", fsiRequest.Input.selinfo.phar_cert_type),
                    new MySqlParameter("@phar_certno", fsiRequest.Input.selinfo.phar_certno),
                    new MySqlParameter("@phar_name", fsiRequest.Input.selinfo.phar_name),
                    new MySqlParameter("@phar_prac_cert_no", fsiRequest.Input.selinfo.phar_prac_cert_no),
                    new MySqlParameter("@hi_feesetl_type", fsiRequest.Input.selinfo.hi_feesetl_type),

                    new MySqlParameter("@setl_id", fsiRequest.Input.selinfo.setl_id),
                    new MySqlParameter("@mdtrt_sn", fsiRequest.Input.selinfo.mdtrt_sn),
                    new MySqlParameter("@psn_no", fsiRequest.Input.selinfo.psn_no),
                    new MySqlParameter("@psn_cert_type", fsiRequest.Input.selinfo.psn_cert_type),
                    new MySqlParameter("@certno", fsiRequest.Input.selinfo.certno),

                    new MySqlParameter("@psn_name", fsiRequest.Input.selinfo.psn_name),
                    new MySqlParameter("@manu_lotnum", fsiRequest.Input.selinfo.manu_lotnum),
                    new MySqlParameter("@manu_date", fsiRequest.Input.selinfo.manu_date),
                    new MySqlParameter("@expy_end", fsiRequest.Input.selinfo.expy_end),
                    new MySqlParameter("@rx_flag", fsiRequest.Input.selinfo.rx_flag),

                    new MySqlParameter("@trdn_flag", fsiRequest.Input.selinfo.trdn_flag),
                    new MySqlParameter("@finl_trns_pric", fsiRequest.Input.selinfo.finl_trns_pric),
                    new MySqlParameter("@rxno", fsiRequest.Input.selinfo.rxno),
                    new MySqlParameter("@rx_circ_flag", fsiRequest.Input.selinfo.rx_circ_flag),
                    new MySqlParameter("@rtal_docno", fsiRequest.Input.selinfo.rtal_docno),

                    new MySqlParameter("@stoout_no", fsiRequest.Input.selinfo.stoout_no),
                    new MySqlParameter("@bchno", fsiRequest.Input.selinfo.bchno),
                    new MySqlParameter("@drug_trac_codg", fsiRequest.Input.selinfo.drug_trac_codg),
                    new MySqlParameter("@drug_prod_barc", fsiRequest.Input.selinfo.drug_prod_barc),
                    new MySqlParameter("@shelf_posi", fsiRequest.Input.selinfo.shelf_posi),

                    new MySqlParameter("@sel_retn_cnt", fsiRequest.Input.selinfo.sel_retn_cnt),
                    new MySqlParameter("@sel_retn_time", fsiRequest.Input.selinfo.sel_retn_time),
                    new MySqlParameter("@sel_retn_opter_name", fsiRequest.Input.selinfo.sel_retn_opter_name),
                    new MySqlParameter("@memo", fsiRequest.Input.selinfo.memo),
                    new MySqlParameter("@mdtrt_setl_type", fsiRequest.Input.selinfo.mdtrt_setl_type),

                    new MySqlParameter("@InputJson", InputJson)
                };
                await MySqlHelper.ExecuteNonQueryAsync(connectionString, query, parameter);
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.Error(
                    $"[GoodsService.GoodsSell]销售上传异常，错误内容：{ex.Message} 异常堆栈{ex.StackTrace}");
                throw;
            }
        }

        /// <summary>
        /// 3506 销售退货上传
        /// </summary>
        /// <param name="fsiRequest">入参</param>
        /// <param name="ControllerName">来源控制器名称</param>
        /// <returns>出参</returns>
        public async Task<bool> GoodsSellReturn(FsiRequestHeader<InputGoodsSell> fsiRequest, string ControllerName)
        {
            try
            {
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                string connectionString =
                    $"Database={ApiDatainfo.mysqlDatabase};Data Source={ApiDatainfo.mysqlUrl};Port={ApiDatainfo.mysqlPost};UserId={ApiDatainfo.mysqlUser};Password={ApiDatainfo.mysqlPwd};Charset=utf8mb4;TreatTinyAsBoolean=false;Allow User Variables=True";
                string InputJson = JsonConvert.SerializeObject(fsiRequest);
                string query = "select * from fsi_goodsSell  where fixmedins_code =@fixmedins_code and med_list_codg =@med_list_codg and fixmedins_hilist_id =@fixmedins_hilist_id and fixmedins_bchno=@fixmedins_bchno and state =0 and datatype=0";
                var parameter = new List<MySqlParameter>
                {
                    new MySqlParameter("@fixmedins_code", fsiRequest.fixmedins_code),
                    new MySqlParameter("@fixmedins_name", fsiRequest.fixmedins_name),
                    new MySqlParameter("@med_list_codg", fsiRequest.Input.selinfo.med_list_codg),
                    new MySqlParameter("@fixmedins_hilist_id", fsiRequest.Input.selinfo.fixmedins_hilist_id),

                    new MySqlParameter("@fixmedins_hilist_name", fsiRequest.Input.selinfo.fixmedins_hilist_name),
                    new MySqlParameter("@fixmedins_bchno", fsiRequest.Input.selinfo.fixmedins_bchno),
                    new MySqlParameter("@prsc_dr_cert_type", fsiRequest.Input.selinfo.prsc_dr_cert_type),
                    new MySqlParameter("@prsc_dr_certno", fsiRequest.Input.selinfo.prsc_dr_certno),
                    new MySqlParameter("@prsc_dr_name", fsiRequest.Input.selinfo.prsc_dr_name),

                    new MySqlParameter("@phar_cert_type", fsiRequest.Input.selinfo.phar_cert_type),
                    new MySqlParameter("@phar_certno", fsiRequest.Input.selinfo.phar_certno),
                    new MySqlParameter("@phar_name", fsiRequest.Input.selinfo.phar_name),
                    new MySqlParameter("@phar_prac_cert_no", fsiRequest.Input.selinfo.phar_prac_cert_no),
                    new MySqlParameter("@hi_feesetl_type", fsiRequest.Input.selinfo.hi_feesetl_type),

                    new MySqlParameter("@setl_id", fsiRequest.Input.selinfo.setl_id),
                    new MySqlParameter("@mdtrt_sn", fsiRequest.Input.selinfo.mdtrt_sn),
                    new MySqlParameter("@psn_no", fsiRequest.Input.selinfo.psn_no),
                    new MySqlParameter("@psn_cert_type", fsiRequest.Input.selinfo.psn_cert_type),
                    new MySqlParameter("@certno", fsiRequest.Input.selinfo.certno),

                    new MySqlParameter("@psn_name", fsiRequest.Input.selinfo.psn_name),
                    new MySqlParameter("@manu_lotnum", fsiRequest.Input.selinfo.manu_lotnum),
                    new MySqlParameter("@manu_date", fsiRequest.Input.selinfo.manu_date),
                    new MySqlParameter("@expy_end", fsiRequest.Input.selinfo.expy_end),
                    new MySqlParameter("@rx_flag", fsiRequest.Input.selinfo.rx_flag),

                    new MySqlParameter("@trdn_flag", fsiRequest.Input.selinfo.trdn_flag),
                    new MySqlParameter("@finl_trns_pric", fsiRequest.Input.selinfo.finl_trns_pric),
                    new MySqlParameter("@rxno", fsiRequest.Input.selinfo.rxno),
                    new MySqlParameter("@rx_circ_flag", fsiRequest.Input.selinfo.rx_circ_flag),
                    new MySqlParameter("@rtal_docno", fsiRequest.Input.selinfo.rtal_docno),

                    new MySqlParameter("@stoout_no", fsiRequest.Input.selinfo.stoout_no),
                    new MySqlParameter("@bchno", fsiRequest.Input.selinfo.bchno),
                    new MySqlParameter("@drug_trac_codg", fsiRequest.Input.selinfo.drug_trac_codg),
                    new MySqlParameter("@drug_prod_barc", fsiRequest.Input.selinfo.drug_prod_barc),
                    new MySqlParameter("@shelf_posi", fsiRequest.Input.selinfo.shelf_posi),

                    new MySqlParameter("@sel_retn_cnt", fsiRequest.Input.selinfo.sel_retn_cnt),
                    new MySqlParameter("@sel_retn_time", fsiRequest.Input.selinfo.sel_retn_time),
                    new MySqlParameter("@sel_retn_opter_name", fsiRequest.Input.selinfo.sel_retn_opter_name),
                    new MySqlParameter("@memo", fsiRequest.Input.selinfo.memo),
                    new MySqlParameter("@mdtrt_setl_type", fsiRequest.Input.selinfo.mdtrt_setl_type),

                    new MySqlParameter("@InputJson", InputJson)
                };
                DataTable dt = await MySqlHelper.ExecuteQueryAsync(connectionString, query, parameter);
                if (dt.Rows.Count < 1)
                {
                    throw new Exception($"查询销售上传记录表失败！根据传入的机构代码【{fsiRequest.fixmedins_code}】医保目录编码【{fsiRequest.Input.selinfo.med_list_codg}】医疗机构内部编码【{fsiRequest.Input.selinfo.fixmedins_hilist_id}】批次号【{fsiRequest.Input.selinfo.fixmedins_bchno}】查询到的销售上传记录总数为：{dt.Rows.Count}");
                }
                query = "INSERT INTO fsi_goodsSell VALUES ( " +
                        " null,@fixmedins_code,@fixmedins_name,@med_list_codg,@fixmedins_hilist_id," +
                        " @fixmedins_hilist_name,@fixmedins_bchno,@prsc_dr_cert_type,@prsc_dr_certno,@prsc_dr_name," +
                        " @phar_cert_type,@phar_certno, @phar_name, @phar_prac_cert_no,@hi_feesetl_type," +
                        " @setl_id,@mdtrt_sn,@psn_no,@psn_cert_type, @certno," +
                        " @psn_name, @manu_lotnum, @manu_date,@expy_end,@rx_flag," +
                        " @trdn_flag, @finl_trns_pric, @rxno,@rx_circ_flag,@rtal_docno," +
                        " @stoout_no, @bchno, @drug_trac_codg,@drug_prod_barc,@shelf_posi," +
                        " @sel_retn_cnt, @sel_retn_time, @sel_retn_opter_name,@memo,@mdtrt_setl_type," +
                        " 0,0,@InputJson, now(),now())";
                await MySqlHelper.ExecuteNonQueryAsync(connectionString, query, parameter);
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.Error(
                    $"[GoodsService.GoodsSellReturn]销售退货上传异常，错误内容：{ex.Message} 异常堆栈{ex.StackTrace}");
                throw;
            }
        }

        /// <summary>
        /// 3506 销售退货上传 数组
        /// </summary>
        /// <param name="fsiRequest">入参</param>
        /// <param name="ControllerName">来源控制器名称</param>
        /// <returns>出参</returns>
        public async Task<bool> GoodsSellReturnList(FsiRequestHeader<InputGoodsSellList> fsiRequest, string ControllerName)
        {
            try
            {
                
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                string connectionString =
                    $"Database={ApiDatainfo.mysqlDatabase};Data Source={ApiDatainfo.mysqlUrl};Port={ApiDatainfo.mysqlPost};UserId={ApiDatainfo.mysqlUser};Password={ApiDatainfo.mysqlPwd};Charset=utf8mb4;TreatTinyAsBoolean=false;Allow User Variables=True";
                string InputJson = JsonConvert.SerializeObject(fsiRequest);

                foreach (var item in fsiRequest.Input.selinfo)
                {
                    string query = "select * from fsi_goodsSell  where fixmedins_code =@fixmedins_code and med_list_codg =@med_list_codg and fixmedins_hilist_id =@fixmedins_hilist_id and fixmedins_bchno=@fixmedins_bchno and state =0 and datatype=0";
                    var parameter = new List<MySqlParameter>
                    {
                        new MySqlParameter("@fixmedins_code", fsiRequest.fixmedins_code),
                        new MySqlParameter("@fixmedins_name", fsiRequest.fixmedins_name),
                        new MySqlParameter("@med_list_codg", item.med_list_codg),
                        new MySqlParameter("@fixmedins_hilist_id", item.fixmedins_hilist_id),

                        new MySqlParameter("@fixmedins_hilist_name", item.fixmedins_hilist_name),
                        new MySqlParameter("@fixmedins_bchno", item.fixmedins_bchno),
                        new MySqlParameter("@prsc_dr_cert_type", item.prsc_dr_cert_type),
                        new MySqlParameter("@prsc_dr_certno", item.prsc_dr_certno),
                        new MySqlParameter("@prsc_dr_name", item.prsc_dr_name),

                        new MySqlParameter("@phar_cert_type", item.phar_cert_type),
                        new MySqlParameter("@phar_certno", item.phar_certno),
                        new MySqlParameter("@phar_name", item.phar_name),
                        new MySqlParameter("@phar_prac_cert_no", item.phar_prac_cert_no),
                        new MySqlParameter("@hi_feesetl_type", item.hi_feesetl_type),

                        new MySqlParameter("@setl_id", item.setl_id),
                        new MySqlParameter("@mdtrt_sn", item.mdtrt_sn),
                        new MySqlParameter("@psn_no", item.psn_no),
                        new MySqlParameter("@psn_cert_type", item.psn_cert_type),
                        new MySqlParameter("@certno", item.certno),

                        new MySqlParameter("@psn_name", item.psn_name),
                        new MySqlParameter("@manu_lotnum", item.manu_lotnum),
                        new MySqlParameter("@manu_date", item.manu_date),
                        new MySqlParameter("@expy_end", item.expy_end),
                        new MySqlParameter("@rx_flag", item.rx_flag),

                        new MySqlParameter("@trdn_flag", item.trdn_flag),
                        new MySqlParameter("@finl_trns_pric", item.finl_trns_pric),
                        new MySqlParameter("@rxno", item.rxno),
                        new MySqlParameter("@rx_circ_flag", item.rx_circ_flag),
                        new MySqlParameter("@rtal_docno", item.rtal_docno),

                        new MySqlParameter("@stoout_no", item.stoout_no),
                        new MySqlParameter("@bchno", item.bchno),
                        new MySqlParameter("@drug_trac_codg", item.drug_trac_codg),
                        new MySqlParameter("@drug_prod_barc", item.drug_prod_barc),
                        new MySqlParameter("@shelf_posi", item.shelf_posi),

                        new MySqlParameter("@sel_retn_cnt", item.sel_retn_cnt),
                        new MySqlParameter("@sel_retn_time", item.sel_retn_time),
                        new MySqlParameter("@sel_retn_opter_name", item.sel_retn_opter_name),
                        new MySqlParameter("@memo", item.memo),
                        new MySqlParameter("@mdtrt_setl_type", item.mdtrt_setl_type),

                        new MySqlParameter("@InputJson", InputJson)
                    };
                    DataTable dt = await MySqlHelper.ExecuteQueryAsync(connectionString, query, parameter);
                    if (dt.Rows.Count < 1)
                    {
                        throw new Exception($"查询销售上传记录表失败！根据传入的机构代码【{fsiRequest.fixmedins_code}】医保目录编码【{item.med_list_codg}】医疗机构内部编码【{item.fixmedins_hilist_id}】批次号【{item.fixmedins_bchno}】查询到的销售上传记录总数为：{dt.Rows.Count}");
                    }
                    query = "INSERT INTO fsi_goodsSell VALUES ( " +
                            " null,@fixmedins_code,@fixmedins_name,@med_list_codg,@fixmedins_hilist_id," +
                            " @fixmedins_hilist_name,@fixmedins_bchno,@prsc_dr_cert_type,@prsc_dr_certno,@prsc_dr_name," +
                            " @phar_cert_type,@phar_certno, @phar_name, @phar_prac_cert_no,@hi_feesetl_type," +
                            " @setl_id,@mdtrt_sn,@psn_no,@psn_cert_type, @certno," +
                            " @psn_name, @manu_lotnum, @manu_date,@expy_end,@rx_flag," +
                            " @trdn_flag, @finl_trns_pric, @rxno,@rx_circ_flag,@rtal_docno," +
                            " @stoout_no, @bchno, @drug_trac_codg,@drug_prod_barc,@shelf_posi," +
                            " @sel_retn_cnt, @sel_retn_time, @sel_retn_opter_name,@memo,@mdtrt_setl_type," +
                            " 0,0,@InputJson, now(),now())";
                    await MySqlHelper.ExecuteNonQueryAsync(connectionString, query, parameter);
                }


                return true;
            }
            catch (Exception ex)
            {
                LogHelper.Error(
                    $"[GoodsService.GoodsSellReturnList]销售退货(数组)上传异常，错误内容：{ex.Message} 异常堆栈{ex.StackTrace}");
                throw;
            }
        }

        /// <summary>
        /// 3507 上传记录删除
        /// </summary>
        /// <param name="fsiRequest">入参</param>
        /// <param name="ControllerName">来源控制器名称</param>
        /// <returns>出参</returns>
        public async Task<bool> GoodsInfoDelete(FsiRequestHeader<InputGoodsInfoDelete> fsiRequest, string ControllerName)
        {
            try
            { 
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                string fixmedins_code = fsiRequest.fixmedins_code;
                string fixmedins_bchno = fsiRequest.Input.data.fixmedins_bchno;
                string inv_data_type = fsiRequest.Input.data.inv_data_type;
                string tablename = string.Empty;
                switch (inv_data_type)
                {
                    case "1":
                        tablename = "fsi_goodsupload";
                        break;
                    case "2":
                        tablename = "fsi_goodsupdate";
                        break;
                    case "3":
                        tablename = "fsi_goodsbuy";
                        break;
                    case "4":
                        tablename = "fsi_goodssell";
                        break;
                    default:
                        throw new Exception($"校验进销存数据类型失败：进销存数据类型{inv_data_type}未被定义！");
                }
                string connectionString = $"Database={ApiDatainfo.mysqlDatabase};Data Source={ApiDatainfo.mysqlUrl};Port={ApiDatainfo.mysqlPost};UserId={ApiDatainfo.mysqlUser};Password={ApiDatainfo.mysqlPwd};Charset=utf8mb4;TreatTinyAsBoolean=false;Allow User Variables=True";
                string query = $"select * from {tablename}  where fixmedins_code =@fixmedins_code and fixmedins_bchno=@fixmedins_bchno and state =0";
                var parameter = new List<MySqlParameter>
                {
                    new MySqlParameter("@fixmedins_code", fsiRequest.fixmedins_code),
                    new MySqlParameter("@fixmedins_bchno", fsiRequest.Input.data.fixmedins_bchno),
                };
                DataTable dt = await MySqlHelper.ExecuteQueryAsync(connectionString, query, parameter);
                if (dt.Rows.Count < 1)
                {
                    throw new Exception(
                        $"查询{tablename}记录表失败！根据传入的机构代码：{fixmedins_code} 上传类型：{inv_data_type}  批次号：{fixmedins_bchno} 查询到的记录总数为：{dt.Rows.Count}");
                }

                query = $"update  {tablename} set state=1,update_time= now() where fixmedins_code =@fixmedins_code and fixmedins_bchno=@fixmedins_bchno and state =0 ";

                await MySqlHelper.ExecuteNonQueryAsync(connectionString, query, parameter);
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.Error($"[GoodsService.GoodsInfoDelete]上传记录删除异常，错误内容：{ex.Message} 异常堆栈{ex.StackTrace}");
                throw;
            }
        }

        /// <summary>
        /// 3568 库存上传(山东特有)
        /// </summary>
        /// <param name="fsiRequest">入参</param>
        /// <param name="ControllerName">来源控制器名称</param>
        /// <returns>出参</returns>
        public async Task<bool> GoodsStocks(FsiRequestHeader<InputGoodStocks> fsiRequest, string ControllerName)
        {
            try
            { 
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                string connectionString =
                    $"Database={ApiDatainfo.mysqlDatabase};Data Source={ApiDatainfo.mysqlUrl};Port={ApiDatainfo.mysqlPost};UserId={ApiDatainfo.mysqlUser};Password={ApiDatainfo.mysqlPwd};Charset=utf8mb4;TreatTinyAsBoolean=false;Allow User Variables=True";
                string InputJson = JsonConvert.SerializeObject(fsiRequest);
                string query =
                    "INSERT INTO `fsi_goodsstocks` (`fixmedins_code`, `fixmedins_name`, `prod_type`, " +
                    "`med_list_codg`, `fixmedins_hilist_id`, `fixmedins_hilist_name`, `inv_cnt`, `pric`, `stockstatus`, `memo`, " +
                    "`vali_flag`, `state`, `InputJson`, `create_time`, `update_time`) VALUES (@fixmedins_code, @fixmedins_name, @prod_type, " +
                    "@med_list_codg, @fixmedins_hilist_id, @fixmedins_hilist_name," +
                    " @inv_cnt, @pric,@stockstatus, @memo, @vali_flag,0, @InputJson, now(), now())";
                var parameter = new List<MySqlParameter>
                {
                    new MySqlParameter("@fixmedins_code", fsiRequest.fixmedins_code),
                    new MySqlParameter("@fixmedins_name", fsiRequest.fixmedins_name),
                    new MySqlParameter("@prod_type", fsiRequest.Input.invinfo.prod_type),
                    new MySqlParameter("@med_list_codg", fsiRequest.Input.invinfo.med_list_codg),
                    new MySqlParameter("@fixmedins_hilist_id", fsiRequest.Input.invinfo.fixmedins_hilist_id),
                    new MySqlParameter("@fixmedins_hilist_name", fsiRequest.Input.invinfo.fixmedins_hilist_name),
                    new MySqlParameter("@inv_cnt", fsiRequest.Input.invinfo.inv_cnt),
                    new MySqlParameter("@pric", fsiRequest.Input.invinfo.pric),
                    new MySqlParameter("@stockstatus", fsiRequest.Input.invinfo.stockstatus),
                    new MySqlParameter("@vali_flag", fsiRequest.Input.invinfo.vali_flag),
                    new MySqlParameter("@memo", fsiRequest.Input.invinfo.memo),
                    new MySqlParameter("@InputJson", InputJson)
                };
                await MySqlHelper.ExecuteNonQueryAsync(connectionString, query, parameter);
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.Error($"[GoodsService.GoodsStocks]库存上传异常，错误内容：{ex.Message} 异常堆栈{ex.StackTrace}");
                throw;
            }
        }

        /// <summary>
        /// 3569 企业信息上传(山东特有)
        /// </summary>
        /// <param name="fsiRequest">入参</param>
        /// <param name="ControllerName">来源控制器名称</param>
        /// <returns>出参</returns>
        public async Task<bool> GoodsInformationInfo(FsiRequestHeader<InputGoodsInformationInfo> fsiRequest, string ControllerName)
        {
            try
            { 
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                string connectionString =
                    $"Database={ApiDatainfo.mysqlDatabase};Data Source={ApiDatainfo.mysqlUrl};Port={ApiDatainfo.mysqlPost};UserId={ApiDatainfo.mysqlUser};Password={ApiDatainfo.mysqlPwd};Charset=utf8mb4;TreatTinyAsBoolean=false;Allow User Variables=True";
                string InputJson = JsonConvert.SerializeObject(fsiRequest);
                string query =
                    "INSERT INTO `fsi_goodsinformationinfo` ( `fixmedins_code`, `fixmedins_name`, `fixmedins_hilist_id`, `entp_code`, " +
                    "`entp_name`, `entp_type`, `busilicperson`, `manager`, `telephone`, `regaddress`, `address`, `province`, `city`, `county`, `city_flag`, " +
                    "`openingbank`, `accountnumber`, `taxcode`, `isreceipttype`, `uscc`, `busilicissue`, `busilicexp`, `busilicissuedate`, `busiliccheckdate`," +
                    " `businessscope`, `qaexpdate`, `qcmanager`, `licence`, `licenceexp`, `licenceissue`, `licencescope`, `licenceissuedate`, `InputJson`, `create_time`, " +
                    "`update_time`) VALUES (@fixmedins_code, @fixmedins_name, @fixmedins_hilist_id, @entp_code, @entp_name, @entp_type,@busilicperson, @manager, " +
                    "@telephone, @regaddress, @address, @province, @city, @county, @city_flag," +
                    " @openingbank, @accountnumber, @taxcode, @isreceipttype, @uscc,@busilicissue, @busilicexp, @busilicissuedate, @busiliccheckdate, " +
                    "@businessscope, @qaexpdate, @qcmanager, @licence, @licenceexp, @licenceissue, @licencescope, @licenceissuedate, @InputJson, now(), now())";

                var parameter = new List<MySqlParameter>
                {
                    new MySqlParameter("@fixmedins_code", fsiRequest.fixmedins_code),
                    new MySqlParameter("@fixmedins_name", fsiRequest.fixmedins_name),
                    new MySqlParameter("@fixmedins_hilist_id", fsiRequest.Input.entpbaseinfo.fixmedins_hilist_id),

                    new MySqlParameter("@entp_code", fsiRequest.Input.entpbaseinfo.entp_code),
                    new MySqlParameter("@entp_name", fsiRequest.Input.entpbaseinfo.entp_name),
                    new MySqlParameter("@entp_type", fsiRequest.Input.entpbaseinfo.entp_type),
                    new MySqlParameter("@busilicperson", fsiRequest.Input.entpbaseinfo.busilicperson),
                    new MySqlParameter("@manager", fsiRequest.Input.entpbaseinfo.manager),
                    new MySqlParameter("@telephone", fsiRequest.Input.entpbaseinfo.telephone),
                    new MySqlParameter("@regaddress", fsiRequest.Input.entpbaseinfo.regaddress),
                    new MySqlParameter("@address", fsiRequest.Input.entpbaseinfo.address),
                    new MySqlParameter("@province", fsiRequest.Input.entpbaseinfo.province),
                    new MySqlParameter("@city", fsiRequest.Input.entpbaseinfo.city),
                    new MySqlParameter("@county", fsiRequest.Input.entpbaseinfo.county),
                    new MySqlParameter("@city_flag", fsiRequest.Input.entpbaseinfo.city_flag),
                    new MySqlParameter("@openingbank", fsiRequest.Input.entpbaseinfo.openingbank),
                    new MySqlParameter("@accountnumber", fsiRequest.Input.entpbaseinfo.accountnumber),
                    new MySqlParameter("@taxcode", fsiRequest.Input.entpbaseinfo.taxcode),
                    new MySqlParameter("@isreceipttype", fsiRequest.Input.entpbaseinfo.isreceipttype),
                    new MySqlParameter("@uscc", fsiRequest.Input.entpbaseinfo.uscc),
                    new MySqlParameter("@busilicissue", fsiRequest.Input.entpbaseinfo.busilicissue),
                    new MySqlParameter("@busilicexp", fsiRequest.Input.entpbaseinfo.busilicexp),
                    new MySqlParameter("@busilicissuedate", fsiRequest.Input.entpbaseinfo.busilicissuedate),
                    new MySqlParameter("@busiliccheckdate", fsiRequest.Input.entpbaseinfo.busiliccheckdate),
                    new MySqlParameter("@businessscope", fsiRequest.Input.entpbaseinfo.businessscope),
                    new MySqlParameter("@qaexpdate", fsiRequest.Input.entpbaseinfo.qaexpdate),
                    new MySqlParameter("@qcmanager", fsiRequest.Input.entpbaseinfo.qcmanager),
                    new MySqlParameter("@licence", fsiRequest.Input.entpbaseinfo.licence),
                    new MySqlParameter("@licenceexp", fsiRequest.Input.entpbaseinfo.licenceexp),
                    new MySqlParameter("@licenceissue", fsiRequest.Input.entpbaseinfo.licenceissue),
                    new MySqlParameter("@licencescope", fsiRequest.Input.entpbaseinfo.licencescope),
                    new MySqlParameter("@licenceissuedate", fsiRequest.Input.entpbaseinfo.licenceissuedate),
                    new MySqlParameter("@InputJson", InputJson)
                };
                await MySqlHelper.ExecuteNonQueryAsync(connectionString, query, parameter);
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.Error($"[GoodsService.GoodsInformationInfo]企业信息上传异常，错误内容：{ex.Message} 异常堆栈{ex.StackTrace}");
                throw;
            }
        }
        
        /// <summary>
        /// 3599 商品追溯码上传(山东特有)
        /// </summary>
        /// <param name="fsiRequest">入参</param>
        /// <param name="ControllerName">来源控制器名称</param>
        /// <returns>出参</returns>
        public async Task<bool> GoodsSellTracInfo(FsiRequestHeader<InputGoodsSellTracInfo> fsiRequest, string ControllerName)
        {
            try
            { 
                string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
                string connectionString =
                    $"Database={ApiDatainfo.mysqlDatabase};Data Source={ApiDatainfo.mysqlUrl};Port={ApiDatainfo.mysqlPost};UserId={ApiDatainfo.mysqlUser};Password={ApiDatainfo.mysqlPwd};Charset=utf8mb4;TreatTinyAsBoolean=false;Allow User Variables=True";
                string InputJson = JsonConvert.SerializeObject(fsiRequest);
                string query =
                    "INSERT INTO `fsi_goodsselltracinfo` ( `fixmedins_code`, `fixmedins_name`, `setl_id`, `setl_flag`, " +
                    "`init_setl_id`, `fixmedins_hilist_id`, `fixmedins_hilist_name`, `med_type`, `mdtrt_sn`, `psn_no`, `psn_cert_type`, `certno`," +
                    " `psn_name`, `bkkp_sn`, `trdn_flag`, `Setl_pric`, `sel_cnt`, `setl_time`, `prcunt`, `memo`, `med_list_codg`, `state`, `InputJson`," +
                    " `create_time`, `update_time`) VALUES ( @fixmedins_code, @fixmedins_name, @setl_id, @setl_flag, @init_setl_id,@fixmedins_hilist_id, @fixmedins_hilist_name," +
                    " @med_type, @mdtrt_sn, @psn_no, @psn_cert_type, @certno, @psn_name, @bkkp_sn,@trdn_flag, @Setl_pric, @sel_cnt," +
                    "@setl_time, @prcunt, @memo, @med_list_codg, 0, @InputJson,now(), now());";
                var parameter = new List<MySqlParameter>
                {
                    new MySqlParameter("@fixmedins_code", fsiRequest.fixmedins_code),
                    new MySqlParameter("@fixmedins_name", fsiRequest.fixmedins_name),
                    new MySqlParameter("@setl_id", fsiRequest.Input.selinfo.setl_id),
                    new MySqlParameter("@setl_flag", fsiRequest.Input.selinfo.setl_flag),
                    new MySqlParameter("@init_setl_id", fsiRequest.Input.selinfo.init_setl_id),
                    new MySqlParameter("@fixmedins_hilist_id", fsiRequest.Input.selinfo.fixmedins_hilist_id),
                    new MySqlParameter("@fixmedins_hilist_name", fsiRequest.Input.selinfo.fixmedins_hilist_name),
                    new MySqlParameter("@med_type", fsiRequest.Input.selinfo.med_type),
                    new MySqlParameter("@mdtrt_sn", fsiRequest.Input.selinfo.mdtrt_sn),
                    new MySqlParameter("@psn_no", fsiRequest.Input.selinfo.psn_no),
                    new MySqlParameter("@psn_cert_type", fsiRequest.Input.selinfo.psn_cert_type),
                    new MySqlParameter("@psn_name", fsiRequest.Input.selinfo.psn_name),
                    new MySqlParameter("@bkkp_sn", fsiRequest.Input.selinfo.bkkp_sn),
                    new MySqlParameter("@trdn_flag", fsiRequest.Input.selinfo.trdn_flag),
                    new MySqlParameter("@Setl_pric", fsiRequest.Input.selinfo.Setl_pric),
                    new MySqlParameter("@sel_cnt", fsiRequest.Input.selinfo.sel_cnt),
                    new MySqlParameter("@setl_time", fsiRequest.Input.selinfo.setl_time),
                    new MySqlParameter("@prcunt", fsiRequest.Input.selinfo.prcunt),
                    new MySqlParameter("@memo", fsiRequest.Input.selinfo.memo),
                    new MySqlParameter("@med_list_codg", fsiRequest.Input.selinfo.med_list_codg),
                    new MySqlParameter("@InputJson", InputJson)
                };
                await MySqlHelper.ExecuteNonQueryAsync(connectionString, query, parameter);
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.Error($"[GoodsService.GoodsSellTracInfo]商品追溯码上传异常，错误内容：{ex.Message} 异常堆栈{ex.StackTrace}");
                throw;
            }
        }
        
        /// <summary>
        ///【3592B】获取虚拟药品追溯码
        /// </summary>
        /// <param name="fsiRequest">入参</param>
        /// <param name="ControllerName">来源控制器名称</param>
        /// <returns></returns>
        public async Task<OutputDummyDrugTracCodg> GetDummyDrugTracCodg(FsiRequestHeader<InputDummyDrugTracCodg> fsiRequest, string ControllerName)
        { 
            try
            {
                if (string.IsNullOrEmpty(fsiRequest.Input.data.med_list_codg)|| string.IsNullOrEmpty(fsiRequest.fixmedins_code)|| string.IsNullOrEmpty(fsiRequest.fixmedins_name)|| string.IsNullOrEmpty(fsiRequest.Input.data.fixmedins_hilist_id)|| string.IsNullOrEmpty(fsiRequest.Input.data.fixmedins_hilist_name)||string.IsNullOrEmpty(fsiRequest.Input.data.fixmedins_bchno))
                {
                    throw new Exception($"参数校验失败：传入的机构代码【{fsiRequest.fixmedins_code}】|机构名称【{fsiRequest.fixmedins_name}】|医疗目录编码【{fsiRequest.Input.data.med_list_codg}】|目录编号【{fsiRequest.Input.data.fixmedins_hilist_id}】|目录名称【{fsiRequest.Input.data.fixmedins_hilist_name}】|批次号【{fsiRequest.Input.data.fixmedins_bchno}】存在为空的情况，请检查入参");
                }
                string connectionString =
                    $"Database={ApiDatainfo.mysqlDatabase};Data Source={ApiDatainfo.mysqlUrl};Port={ApiDatainfo.mysqlPost};UserId={ApiDatainfo.mysqlUser};Password={ApiDatainfo.mysqlPwd};Charset=utf8mb4;TreatTinyAsBoolean=false;Allow User Variables=True";
                Random Random = new Random();
                // 获取当前时间戳（毫秒级）
                long timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                // 生成剩余位数的随机数，总共需要20位
                int remainingDigits = 20 - timestamp.ToString().Length;
                string randomSuffix = "";
                for (int i = 0; i < remainingDigits; i++)
                {
                    randomSuffix += Random.Next(0, 10);
                }
                // 拼接并转换为长整型返回
                string uniqueNumberStr = timestamp + randomSuffix;

                string query =
                    "insert into fsi_dummydrugtraccodg(fixmedins_code,fixmedins_name, med_list_codg, fixmedins_hilist_id, fixmedins_hilist_name, fixmedins_bchno, memo, drug_trac_codg, create_time, update_time) values (@fixmedins_code,@fixmedins_name,@med_list_codg, @fixmedins_hilist_id, @fixmedins_hilist_name, @fixmedins_bchno, @memo, @drug_trac_codg, now(), now()) ";
                var parameter = new List<MySqlParameter>
                {
                    new MySqlParameter("@fixmedins_code", fsiRequest.fixmedins_code),
                    new MySqlParameter("@fixmedins_name", fsiRequest.fixmedins_name),
                    new MySqlParameter("@med_list_codg", fsiRequest.Input.data.med_list_codg),
                    new MySqlParameter("@fixmedins_hilist_id", fsiRequest.Input.data.fixmedins_hilist_id),
                    new MySqlParameter("@fixmedins_hilist_name", fsiRequest.Input.data.fixmedins_hilist_name),
                    new MySqlParameter("@fixmedins_bchno", fsiRequest.Input.data.fixmedins_bchno),
                    new MySqlParameter("@memo", fsiRequest.Input.data.memo),
                    new MySqlParameter("@drug_trac_codg", uniqueNumberStr)
                };
                await MySqlHelper.ExecuteNonQueryAsync(connectionString, query, parameter);
                var output = new OutputDummyDrugTracCodg()
                {
                    result = new OutputDataDummyDrugTracCodg()
                    {
                        med_list_codg = fsiRequest.Input.data.med_list_codg,
                        fixmedins_hilist_id = fsiRequest.Input.data.fixmedins_hilist_id,
                        fixmedins_hilist_name = fsiRequest.Input.data.fixmedins_hilist_name,
                        drug_trac_codg = uniqueNumberStr   
                    }
                };
                return output;
            }
            catch (Exception ex)
            {
                LogHelper.Error($"[GoodsService.GetDummyDrugTracCodg]获取虚拟追溯码异常，错误内容：{ex.Message} 异常堆栈{ex.StackTrace}");
                throw;
            }
        }
    }
}